package org.uim.cricket.analyzer.validate.impl;

import java.util.*;

import org.uim.cricket.analyzer.validate.VLogger;
import org.uim.cricket.analyzer.validate.ValidatableMatch;
import org.uim.cricket.entities.MatchPlayer;
import org.uim.cricket.entities.aggr.SingleInning;
import org.uim.cricket.entities.bowling.BowlerStatistics;
import org.uim.cricket.entities.bowling.BowlingInning;
import org.uim.cricket.entities.match.CricketMatch;

public class BowlerValidators implements ValidatableMatch {
	
	@Override
	public void processValidation(CricketMatch match, Map<String, List<MatchPlayer>> players) throws Exception {
		
		if (match.getInnings() == null) return;
        
        SingleInning[] inns = match.getInnings();
        
        for (int i=0; i<inns.length; i++) {
        	if (inns[i] == null) continue;
        	
            if (inns[i].getBowlInnings() != null) {
                this.processBowlings(inns[i].getBowlInnings(), match, (i+1));
            }
        }
        
	}

	private void processBowlings(List<BowlingInning> bowlInns, CricketMatch match, int inning) {
		
		SingleInning inn = match.getInnings()[inning - 1];
        
        for (int i=0; i<bowlInns.size(); i++) {
            BowlingInning bowl = bowlInns.get(i);
            if (bowl == null) continue;
            
            BowlerStatistics stats = bowl.getStatistics();
            
            if (stats == null) {
            	VLogger.warn("Inning #" + inning + ": Bowler: " + bowl.getBowler().getTitleName() +
            					", doesn't have any bowling statistics!");
            	continue;
            }
            
            int ovs = (int) Double.parseDouble(stats.getOvers());
            if (ovs < stats.getMaidnes()) {
            	VLogger.warn("Inning #" + inning + ": Bowler: " + bowl.getBowler().getTitleName() +
								", has maidnes more than his overs!");
            	VLogger.info("[Overs: " + stats.getOvers() + " < Maidnes: " + stats.getMaidnes());
            }
            
            if (inn != null && inn.getTotals() != null) {
	            if (stats.getWickets() > inn.getTotals().getWickets()) {
	            	VLogger.warn("Inning #" + inning + ": Bowler: " + bowl.getBowler().getTitleName() +
									", has more wickets than its inning!");
	            	VLogger.info("[Inning-Wickets: " + inn.getTotals().getWickets() + " < Wickets: " + stats.getWickets());
	            }
            }
            
            int ex = stats.getNoballs() + stats.getWides();
            if (stats.getRuns() < ex) {
            	VLogger.warn("Inning #" + inning + ": Bowler: " + bowl.getBowler().getTitleName() +
								", has extras more than runs conceeded!");
            	VLogger.info("[Runs-conceeded: " + stats.getRuns() + " < Extras-conceeded: " + ex);
            }
            
        }
		
	}
	
}
